package com.self.cgqcompose.ui.components

import android.content.Intent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.indication
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Icon
import androidx.compose.material3.SecondaryScrollableTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.self.cgqcompose.R
import com.self.cgqcompose.beans.HomeTopTabs
import com.self.cgqcompose.mock.MockJson
import com.self.cgqcompose.ui.TestActivity

@Composable
fun HomeTopBar(tabs: HomeTopTabs, selectedIndex: Int = 0, onItemSelected: (Int) -> Unit = {}) {
    Box(
        modifier = Modifier
            .clickable(
                interactionSource = remember { MutableInteractionSource() },
                indication = null
            ) {}
            .fillMaxWidth()
            .height(132.dp)
    ) {
        Image(
            painter = painterResource(id = R.mipmap.home_top_bg), contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight(), contentScale = ContentScale.FillBounds
        )

        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(top = 35.dp)
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically, modifier = Modifier
                    .fillMaxWidth()
                    .height(44.dp)
            ) {
                val context = LocalContext.current
                Image(
                    painter = painterResource(id = R.mipmap.home_logo_5g),
                    contentDescription = null,
                    modifier = Modifier
                        .padding(horizontal = 8.dp)
                        .clickable {
                            val intent = Intent(context, TestActivity::class.java)
                            context.startActivity(intent)
                        }
                )
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .weight(1f)
                ) {
                    var value by rememberSaveable { mutableStateOf("initial") }
                    BasicTextField(value = value, onValueChange = { value = it },
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(30.dp),
                        textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
                        decorationBox = { innerTextField ->
                            Row(
                                Modifier
                                    .background(Color.White, RoundedCornerShape(percent = 30))
                                    .padding(6.dp), verticalAlignment = Alignment.CenterVertically
                            ) {
                                Icon(
                                    painter = painterResource(R.mipmap.hone_search_icon),
                                    contentDescription = null
                                )
                                Spacer(Modifier.width(16.dp))
                                innerTextField()
                            }
                        })
                    if (value.isEmpty()) {
                        Text(
                            text = "Placeholder",
                            modifier = Modifier
                                .align(Alignment.CenterStart)
                                .padding(start = 40.dp),
                            color = Color.Gray
                        )
                    }
                }
                Image(
                    painter = painterResource(id = R.mipmap.home_logo_5g),
                    contentDescription = null,
                    modifier = Modifier.padding(horizontal = 8.dp)
                )
            }
            SecondaryScrollableTabRow(
                selectedTabIndex = selectedIndex,
                edgePadding = 0.dp,
                containerColor = Color.Transparent,
                indicator = {},
                divider = {},
                minTabWidth = 0.dp
            ) {
                tabs.tabs.forEachIndexed { index, tab ->
                    Tab(
                        selected = selectedIndex == index,
                        onClick = {
                            onItemSelected(index)
                        },
                        modifier = Modifier
                            .wrapContentWidth()
                            .clickable(
                                interactionSource = remember { MutableInteractionSource() },
                                indication = null
                            ) {}
                            .padding(10.dp)
                    ) {
                        Text(
                            text = tab.name,
                            color = if (selectedIndex == index) Color.Black else Color.Gray,
                            fontSize = if (selectedIndex == index) 19.sp else 15.sp,
                            fontWeight = if (selectedIndex == index) FontWeight.Bold else FontWeight.Normal,
                        )
                    }
                }
            }
        }
    }
}

@Preview
@Composable
fun HomeTopBarPreview() {
    val context = LocalContext.current;
    val tabs = MockJson.loadMockDataTopTabs(context)
    HomeTopBar(tabs, 0)
}
